import request from '@/utils/request'

type Obj = Record<string, any>

export function list(query:Obj, sort?:string) {
  let url = ''
  if(sort === '默认'){
    url = '/system/goldVs/list'
  } else if(sort === '升序'){
    url = '/system/goldVs/listAsc'
  } else if(sort === '降序'){
    url = '/system/goldVs/listDesc'
  }

  return request({
    url,
    method: 'get',
    params: query
  })
}

// type res = {
//   [x: string]: any
// }

const _titleList = [
  '电池组件产能', '电池片产能',
  '综合效率', '设备节拍', '性能稼动率', '时间稼动率', '良率',
  '总损失率', '总待机率', '总停机率', '总Down率', '总PM率', '总OFF率',
  '设备故障率(%)', '原材料故障率(%)', '辅材料故障率(%)', 'Interlock率(%)'
]

const cellStyle = ({ rowIndex, value }: Obj) => {
  if(rowIndex === 0 && _titleList.includes(value)){
      return {
        background: '#fce4d6'
      }
  }
}

const cellStyleColor = ({ data, value }: Obj) => {
  if(data.eqpAttribute1 === '实绩-目标' && Number(value) < 0){
    return {
      color: 'red',
      fontWeight: 'bold'
    }
  }
}

const cellStyleColorFirst = ({ data, value, rowIndex }: Obj) => {
  if(data.eqpAttribute1 === '实绩-目标' && Number(value) < 0){
    return {
      color: 'red',
      fontWeight: 'bold'
    }
  }

  if(!rowIndex){
    return {
      color: 'blue',
      fontWeight: 'bold',
      textDecoration: 'underline',
      cursor: 'pointer'
    }
  }
}

export const columnsDiewa = [
  { headerName: "eqpAttribute1", field: "eqpAttribute1", minWidth: 130, cellStyle, tooltipField:'eqpAttribute1' },
  { headerName: "eqpAttribute2", field: "eqpAttribute2", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute3", field: "eqpAttribute3", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute4", field: "eqpAttribute4", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute5", field: "eqpAttribute5", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute6", field: "eqpAttribute6", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute7", field: "eqpAttribute7", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute8", field: "eqpAttribute8", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute9", field: "eqpAttribute9", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute10", field: "eqpAttribute10", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute11", field: "eqpAttribute11", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute12", field: "eqpAttribute12", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute13", field: "eqpAttribute13", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute14", field: "eqpAttribute14", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute15", field: "eqpAttribute15", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute16", field: "eqpAttribute16", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute17", field: "eqpAttribute17", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute18", field: "eqpAttribute18", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute19", field: "eqpAttribute19", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute20", field: "eqpAttribute20", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute21", field: "eqpAttribute21", minWidth: 45, cellStyle: cellStyleColor }
]

export const firstColumnMBB = [
  { headerName: "eqpAttribute1", field: "eqpAttribute1", minWidth: 130, cellStyle, tooltipField:'eqpAttribute1' },
  { headerName: "eqpAttribute2", field: "eqpAttribute2", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute3", field: "eqpAttribute3", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute4", field: "eqpAttribute4", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute5", field: "eqpAttribute5", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute6", field: "eqpAttribute6", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute7", field: "eqpAttribute7", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute8", field: "eqpAttribute8", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute9", field: "eqpAttribute9", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute10", field: "eqpAttribute10", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute11", field: "eqpAttribute11", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute12", field: "eqpAttribute12", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute13", field: "eqpAttribute13", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute14", field: "eqpAttribute14", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute15", field: "eqpAttribute15", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute16", field: "eqpAttribute16", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute17", field: "eqpAttribute17", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute18", field: "eqpAttribute18", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute19", field: "eqpAttribute19", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute20", field: "eqpAttribute20", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute21", field: "eqpAttribute21", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute22", field: "eqpAttribute22", minWidth: 45, cellStyle: cellStyleColorFirst },
  { headerName: "eqpAttribute23", field: "eqpAttribute23", minWidth: 45, cellStyle: cellStyleColorFirst }
]

export const columnMBB = [
  ...columnsDiewa,
  { headerName: "eqpAttribute22", field: "eqpAttribute22", minWidth: 45, cellStyle: cellStyleColor },
  { headerName: "eqpAttribute23", field: "eqpAttribute23", minWidth: 45, cellStyle: cellStyleColor }
]

export const getTableData = (data:any[]) => {

  let _tempArr = ['', '目标', '实绩', '实绩-目标', '微故障率_60秒', '小故障率_10分', '中故障率_1时', '大故障率_4时', '特大故障率_4时+'] // down指标定制

  data.forEach((val:any) => {
    val.goldVsReturnList.forEach((item:any, idx:number) => {
      if(item.eqpAttribute1 === '设备'){ /** 截去 H05- */
        for (let key in item) {
          if(item[key]){
            item[key] = item[key].includes('H05') ? item[key].slice(4) : item[key]
          }
        }

      }
      if(item.eqpAttribute1 === '设备'){
        item.eqpAttribute1 = val.type
      } else if(!item.eqpAttribute1.includes('设备故障率')) {
        item.eqpAttribute1 = (item.eqpAttribute1.length >= 2 ? item.eqpAttribute1.slice(-2) : item.eqpAttribute1) === '差异' ? '实绩-目标' : (item.eqpAttribute1.length >= 2 ? item.eqpAttribute1.slice(-2) : item.eqpAttribute1)
      } else {
        item.eqpAttribute1 = _tempArr[idx]
      }
    })
  })

  return data
}

export const chartOptios = (data:any[], idx: number, type:string, machine:string, time:string, sortable?:string) => {
  let legendData:string[] = []
  let xAxisData:any[] = (Object.values(data[0].goldVsReturnList[0]).slice(1)).slice(0, 22)
  let _target:string[] = []
  let _result:string[] = []
  let _completed:string[] = []
  let _minute:string[] = []
  let _small:string[] = []
  let _centre:string[] = []
  let _big:string[] = []
  let _large:string[] = []

  if(data[idx].goldVsReturnList.length === 4){
    legendData = ['实绩-目标', '目标', '实绩']
  } else {
    legendData = ['实绩-目标', '目标', '实绩', '微故障率_60秒', '小故障率_10分', '中故障率_1时', '大故障率_4时', '特大故障率_4时+']
  }

  data.forEach((item:any) => {
    if(item.type === type){

      item.goldVsReturnList.forEach((val:Obj) => {
        if(val.eqpAttribute1 === '目标'){
          _target = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '实绩'){
          _result = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '实绩-目标'){
          _completed = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '微故障率_60秒'){
          _minute = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '小故障率_10分'){
          _small = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '中故障率_1时'){
          _centre = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '大故障率_4时'){
          _big = Object.values(val).slice(1)
        }

        if(val.eqpAttribute1 === '特大故障率_4时+'){
          _large = Object.values(val).slice(1)
        }

      })
    }
  })

  return {
    title: {
      text: `${machine} ${type}_${time}`,
      textStyle: {
        fontSize:'16px',
        fontWeight: 500
      }
    },
    legend: {
      right: '50',
      data: legendData
    },
    toolbox: {
      feature: {
        saveAsImage: {}, // 保存图片
        brush: {
          type: ['']
        }
      }
    },
    grid: [
      {
        left: '130px',
        right: '10px',
        height: '18%',
        top:'18%'
      },
      {
        left: '130px',
        right: '10px',
        top: '40%',
        height: '57%',
        bottom: '0px'
      }
    ],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      borderWidth: 1,
      borderColor: '#ccc',
      padding: 10,
      textStyle: {
        color: '#000',
        fontSize: 12
      }
    },
    // color: _colors,
    xAxis: [
      {
        type: 'category',
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false // 不显示坐标轴线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        },
        data: xAxisData // 横坐标的数据
      },
      {
        gridIndex: 1,
        type: 'category',
        data: xAxisData,
        boundaryGap: true,
        axisLine: { onZero: false },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLabel: {
          show: false // 不显示坐标轴上的文字
        }
        // min: 'dataMin',
        // max: 'dataMax'
      }
    ],
    yAxis: [
      {
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
      },
      {
        gridIndex: 1,
        type: 'value',
        axisLabel: {
          textStyle: {
            color: '#fff'
          }
        },
        splitLine: {
          show: false
        },
        axisLine: {
          show: false
        },
        scale: true
      }
    ],
    series: [
      {
        name: '实绩-目标',
        type: 'bar',
        barWidth: '50%',
        label: {
          show: true,
          position: 'insideBottom'
        },
        itemStyle: {
          color: '#ededed'
        },
        data: _completed

      },
      {
        name: '目标',
        type: 'line',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        // label: {
        //   show: true,
        //   position: 'top'
        // },
        lineStyle: {
          width: 1
        },
        symbolSize: 2,
        data: _target
      },
      {
        name: '实绩',
        type: type === '设备故障率(%)' ? 'line': 'bar',
        stack: type === '设备故障率(%)' ? '' : '总量',
        barWidth: '50%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        itemStyle: {
          // color: '#bdd7ee'
        },
        label: {
          show: true,
          position: 'top'
        },
        data: _result
      },
      {
        name: '微故障率_60秒',
        type: 'bar',
        stack: '总量',
        barWidth: '50%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        itemStyle: {
          // color: '#bdd7ee'
        },
        data: _minute
      },
      {
        name: '小故障率_10分',
        type: 'bar',
        stack: '总量',
        barWidth: '50%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        itemStyle: {
          // color: '#bdd7ee'
        },
        data: _small
      },
      {
        name: '中故障率_1时',
        type: 'bar',
        stack: '总量',
        barWidth: '50%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        itemStyle: {
          // color: '#bdd7ee'
        },
        data: _centre
      },
      {
        name: '大故障率_4时',
        type: 'bar',
        stack: '总量',
        barWidth: '50%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        itemStyle: {
          // color: '#bdd7ee'
        },
        data: _big
      },
      {
        name: '特大故障率_4时+',
        type: 'bar',
        stack: '总量',
        barWidth: '50%',
        xAxisIndex: 1,
        yAxisIndex: 1,
        smooth: true,
        itemStyle: {
          // color: '#bdd7ee'
        },
        data: _large
      }

    ]
  }
}